<?php

namespace Yiru\LaravelComposer\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Log;
use DB;
use Exception;

use Josh\Terminal\TerminalFacade    as Terminal;
use mikehaertl\shellcommand\Command as Shell;               //https://github.com/mikehaertl/php-shellcommand
use October\Rain\Config\DataWriter\Rewrite as Config;
use Webimpress\SafeWriter\FileWriter;

class ComposerRemove extends Command
{    
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'composer:remove {pkgs}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Remove pacakge, clean config/migration/view/table etc';
    
    public function __construct()
    {
        parent::__construct();        
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
         $pkgs = $this->argument('pkgs')?:null;
        // dump(config('composer'));
        if($pkgs=='all'){
            [$pkgs,$configs] = Arr::divide(config('composer'));
        }else $pkgs = explode(',',$pkgs);
        echo PHP_EOL;    
        foreach ($pkgs as $pkg) {
             echo $pkg.PHP_EOL;
        }
        echo PHP_EOL;    
        $this->undo_require($pkgs);
        // $this->repo($pkgs);        
        // $this->publish($pkgs);        
        // $this->config($pkgs);
        // $this->env($pkgs);
        // if(!empty($pkgs))   
    }

    /**
     * Composer Remove package
     * @return bool
     */
    public function undo_require($pkgs)
    {
        $reqPkgs="";
        if(!is_array($pkgs)) $pkgs=[$pkgs];
        foreach ($pkgs as $pkg) {
            $ver=config('composer.'.$pkg.'.ver');
            echo PHP_EOL.$pkg.' ver:'.$ver;
            if(!empty($ver))$pkg = Str::of($pkg)->append(':'.$ver);
            // $pkg    = Str::of($pkg)->append(config('composer.'.$pkg.'.ver'));            
            $reqPkgs= Str::of($reqPkgs)->append(" ".$pkg);
            echo PHP_EOL.$pkg;
        }
        $this->output->note('composer remove -vvv '.$reqPkgs);
        $command= new Shell('composer remove -vvv '.$reqPkgs);
        if($command->execute()){
            $ret=$command->getOutput(); 
            $this->output->success($ret);  
        }else{
            $ret=$command->getError();
            $this->output->error($ret);  
            $exitCode = $command->getExitCode();
            throw new Exception('Composer remove failed');
        }

        $command= new Shell('composer dumpautoload');
        $command->execute();
    }

    protected function getOptions()
    {
        return [
            ['config',   'c', InputOption::VALUE_OPTIONAL, 'remove config'],
            ['env',      'e', InputOption::VALUE_OPTIONAL, 'remove env'],
            ['publish',  'p', InputOption::VALUE_OPTIONAL, 'remove publish'],
            ['route',    'r', InputOption::VALUE_OPTIONAL, 'remove route'],
            ['migration','m', InputOption::VALUE_OPTIONAL, 'remove migration']            
        ];
    }

}
